package com.ypbase.cloud.tags.api.impl;

import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.rpc.BaseDubboException;
import com.alibaba.dubbo.rpc.BaseDubboServerException;
import com.cloud.framework.core.log.LoggerFactory;
import com.cloud.framework.core.utils.ToolKit;
import com.ypbase.cloud.tags.api.ITagCategoryApi;
import com.ypbase.cloud.tags.api.dto.TagCategoryDto;
import com.ypbase.cloud.tags.common.dictionary.TagExceptionEnum;
import com.ypbase.cloud.tags.entity.TagCategory;
import com.ypbase.cloud.tags.service.ITagCategoryService;

public class TagCategoryApiImpl implements ITagCategoryApi {

	private static final Logger logger = LoggerFactory.getLogger(TagCategoryApiImpl.class);

	@Autowired
	private ITagCategoryService tagCategoryService;

	@Override
	public List<TagCategoryDto> getTagCategoryList(Long companyId) throws BaseDubboServerException, BaseDubboException {
		if (companyId == null) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "companyId"));
		}
		TagCategory tagCategory = new TagCategory();
		tagCategory.setCompanyId(companyId);
		return tagCategoryService.geTagCategoryList(tagCategory);
	}

	@Override
	public void addTagCategory(TagCategoryDto tagCategoryDto) throws BaseDubboServerException, BaseDubboException {
		if (tagCategoryDto.getCompanyId() == null) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "companyId"));
		}
		if (ToolKit.isNullOrBlank(tagCategoryDto.getNameCn())) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "nameCn"));
		}

		// 校验新增的数据是否重复
		TagCategory tagCategory = new TagCategory();
		tagCategory.setCompanyId(tagCategoryDto.getCompanyId());
		tagCategory.setNameCn(tagCategoryDto.getNameCn());
		List<TagCategoryDto> modelList = tagCategoryService.geTagCategoryList(tagCategory);
		if (modelList.size() > 0) {
			throw new BaseDubboException(TagExceptionEnum.REPEAT.getCode(),
					String.format(TagExceptionEnum.REPEAT.getMsg()));
		}

		try {
			// 添加分类
			tagCategoryService.addTagCategory(tagCategory);

		} catch (Throwable e) {
			e.printStackTrace();
			logger.error(e.toString());
			throw new BaseDubboServerException(TagExceptionEnum.SYSTEM_ERROR.getCode(),
					TagExceptionEnum.SYSTEM_ERROR.getMsg());
		}
	}

	@Override
	public void deleteTagCategory(Long companyId) throws BaseDubboServerException, BaseDubboException {
		if (companyId == null) {
			throw new BaseDubboException(TagExceptionEnum.PARAM_MUST_INPUT.getCode(),
					String.format(TagExceptionEnum.PARAM_MUST_INPUT.getMsg(), "companyId"));
		}
//		if() {
//			
//		}
		
		tagCategoryService.deleteTagCategory(companyId);
	}
}
